package com.cx.interact.server.lottery.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.cx.base.apiModel.PageModel;
import com.cx.base.dao.AbstractBaseDao;
import com.cx.base.exception.SoaException;
import com.cx.base.utils.IntegerUtil;
import com.cx.interact.interfaces.lottery.dto.lottery.PrizeDTO;
import com.cx.interact.interfaces.lottery.dto.lottery.PrizeTypeDTO;

@Repository
public class PrizeDAO extends AbstractBaseDao{

	/**
	 * 查询活动奖项类别列表
	 * */
	public List<PrizeTypeDTO> getPrizeType() throws Exception{
		String sql = " select * from iat_prize_type ";
		
		List<Object> parameter = new ArrayList<Object>();
		
		PageModel<PrizeTypeDTO> pageModel = getPageList(1, 10000, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(PrizeTypeDTO.class));
		
		if(pageModel != null && pageModel.getItems() != null ){
			return pageModel.getItems();
		}else{
			return null;
		}
	}
	
	/**
	 * 保存或更新活动奖项信息列表
	 * */
	public Boolean saveOrUpdatePrize(PrizeDTO prizeDTO) throws Exception{
		if(prizeDTO != null	&& prizeDTO.getActId() > 0){
			Integer remainder = IntegerUtil.getTwentyRemainder(prizeDTO.getActId());
			
			if(prizeDTO.getPrizeId() > 0){
				update(prizeDTO, "iat_prize_"+remainder, "prize_id", true);
			}else{
				insert(prizeDTO, "iat_prize_"+remainder, "prize_id", true);
			}
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * 查询活动奖项信息列表
	 * */
	public List<PrizeDTO> getPrize(Integer actId) throws Exception{
		if(actId > 0){
			Integer remainder = IntegerUtil.getTwentyRemainder(actId);
			String sql = " select * from iat_prize_"+remainder+" where actId = ? and del_status = 0 ";
			
			List<Object> parameter = new ArrayList<Object>();
			parameter.add(actId);
			
			PageModel<PrizeDTO> pageModel = getPageList(1, 10000, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(PrizeDTO.class));
			
			if(pageModel != null && pageModel.getItems() != null ){
				return pageModel.getItems();
			}
		}
		return null;
	}
	
	/**
	 * 删除活动奖项信息列表
	 * */
	public Boolean delPrize(Integer actId,Integer prizeId) throws Exception{
		if(actId > 0 && prizeId > 0){
			Integer remainder = IntegerUtil.getTwentyRemainder(actId);
			
			String sql = " update iat_prize_"+remainder+" set del_status = 1 where act_id = ? and prize_id = ? ";
			
			jdbcTemplate.update(sql, actId,prizeId);
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * 查询活动奖项(存缓存)
	 * */
	public List<PrizeDTO> getPrizeByCache(Integer actId) throws Exception{
		if(actId > 0){
			Integer remainder = IntegerUtil.getTwentyRemainder(actId);
			String sql = " select * from iat_prize_"+remainder+" where actId = ? and del_status = 0 ";
			
			List<Object> parameter = new ArrayList<Object>();
			parameter.add(actId);
			
			PageModel<PrizeDTO> pageModel = getPageList(1, 10000, sql, parameter.toArray(), ParameterizedBeanPropertyRowMapper.newInstance(PrizeDTO.class));
			
			if(pageModel != null && pageModel.getItems() != null ){
				return pageModel.getItems();
			}
		}
		return null;
	}
}
